package cn.iocoder.yudao.module.dbit.service.stumst;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.dbit.controller.admin.stumst.vo.StuMstRespVO;
import cn.iocoder.yudao.module.dbit.dal.dataobject.jobinfo.JobInfoDO;
import cn.iocoder.yudao.module.dbit.dal.dataobject.stumst.StuMstDO;
import cn.iocoder.yudao.module.dbit.dal.mysql.stumst.StuMstMapper;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.JOB_INFO_NOT_EXISTS;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.STUMST_INFO_NOT_EXISTS;


/**
 * 简历投递 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StuMstServiceImpl implements StuMstService {

    private static final Logger log = LoggerFactory.getLogger(StuMstServiceImpl.class);
    @Resource
    private AdminUserService adminUserService;

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private StuMstMapper stuMstMapper;

    @Override
    public StuMstDO getStuMst(Long id) {
        return stuMstMapper.selectById(id);
    }

    @Override
    public StuMstRespVO getStuMstInfo(Long id) {
        StuMstRespVO respVO = null;
        try {
            StuMstDO stuMstDO = stuMstMapper.selectById(id);
            if (stuMstDO != null) {
                respVO = BeanUtils.toBean(stuMstDO, StuMstRespVO.class);
                setUserInfo(respVO, id);
            }
        } catch (Exception e) {
            // 处理异常
            System.err.println("Error occurred while fetching student information: " + e.getMessage());
        }
        return respVO;
    }

    private void setUserInfo(StuMstRespVO respVO, Long id) {
        AdminUserDO user = adminUserService.getUser(id);
        if (user != null) {
            respVO.setNickname(user.getNickname());
            respVO.setAvatar(user.getAvatar());
            respVO.setSex(user.getSex());
        }
    }

    @TenantIgnore
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStuMstInfo(StuMstRespVO updateReqVO) {
        // 校验存在
        validateStuMstInfoExists(updateReqVO.getStuId());
        // 更新
        StuMstDO stuMstObj = cn.iocoder.yudao.framework.common.util.object.BeanUtils.toBean(updateReqVO, StuMstDO.class);
        AdminUserDO adminUserObj = cn.iocoder.yudao.framework.common.util.object.BeanUtils.toBean(updateReqVO, AdminUserDO.class);
        adminUserObj.setId(Long.valueOf(stuMstObj.getStuId()));
        adminUserMapper.updateById(adminUserObj);
        stuMstMapper.updateById(stuMstObj);

    }

    private void validateStuMstInfoExists(Integer id) {
        if (stuMstMapper.selectById(id) == null) {
            throw exception(STUMST_INFO_NOT_EXISTS);
        }
    }
}